package com.monolith.jd.demo.service.impl;

import com.monolith.jd.demo.common.entity.PackageEntity;
import com.monolith.jd.demo.common.vo.JdRequestData;
import com.monolith.jd.demo.common.vo.PackageVo;
import com.monolith.jd.demo.dao.IntegerIdDao;
import com.monolith.jd.demo.dao.PackageDao;
import com.monolith.jd.demo.service.PackageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 2018/10/27 Create by 郭文梁
 * PackageServiceImpl
 * 包裹业务逻辑实现
 *
 * @author 郭文梁
 * @date 2018/10/27
 */
@Service
public class PackageServiceImpl extends BaseIntegerIdServiceImpl<PackageEntity> implements PackageService {
    private final PackageDao packageDao;

    @Autowired
    public PackageServiceImpl(PackageDao packageDao) {
        this.packageDao = packageDao;
    }

    @Override
    public IntegerIdDao<PackageEntity> getRepository() {
        return packageDao;
    }

    @Override
    public void clear() {
        packageDao.deleteAll();
    }

    @Override
    public boolean saveData(JdRequestData requestData) {
        List<PackageVo> packageVos = requestData.getOperBody();
        for (PackageVo packageVo : packageVos) {
            PackageEntity packageEntity = new PackageEntity();
            BeanUtils.copyProperties(packageVo, packageEntity);
            PackageEntity result = save(packageEntity);
            if (result == null) {
                return false;
            }
        }
        return true;
    }
}
